package com.yxhl.zoume.core.func.appraisal.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.appraisal.view.AppraisalResultView;
import com.yxhl.zoume.data.http.rest.param.appraisal.AppraisalResultParam;
import com.yxhl.zoume.data.http.rest.response.appraisal.AppraisalResultResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.appraisal.AppraisalResultUseCase;

import javax.inject.Inject;

/**
 * Created by vhawk on 16/9/20.
 */

public class AppraisalResultPresenter extends BasePresenter {

    @Inject
    public AppraisalResultUseCase useCase;
    private AppraisalResultView view;

    @Inject
    public AppraisalResultPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (AppraisalResultView) view;
    }

    /**
     * 获取评论结果
     *
     * @param orderId
     */
    public void fetchConfirmResult(String orderId) {
        AppraisalResultParam param = new AppraisalResultParam();
        param.setId(orderId)
                .setMobile(getMobile())
                .setToken(getToken());
        useCase.execute(param, new ZMSubscriber<AppraisalResultResponse>() {
            @Override
            public void onNext(AppraisalResultResponse response) {
                super.onNext(response);
                if (response.isSucc()) {
                    view.handleAppraisalResultSucceed(response);
                } else {
                    view.handleAppraisalFailure(response.getResultMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.handleAppraisalError();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        useCase.unsubscribe();
    }
}
